#include<vector>
using namespace std;

class Solution {

    int findParent(int x,vector<int>&p) {
        return x == p[x] ? x : (p[x] = findParent(p[x], p));
    }

    bool merge(int x, int y, vector<int>& p, vector<int>&ans) {
        int px = findParent(x,p);
        int py = findParent(y,p);
        if (px == py) {
            ans[0] = x;
            ans[1] = y;
            return true;
        }
        p[px] = py;
        return false;
    }

public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        vector<int>p(edges.size()+1);
        for (int i = 1; i < p.size(); ++i)
            p[i] = i;
        vector<int>ans(2);
        for (auto& edge : edges) {
            if (merge(edge[0], edge[1], p, ans))
                return ans;
        }
        return ans;

    }
};